use indicatif::{ProgressBar, ProgressStyle, MultiProgress};
use std::time::Duration;

pub struct ConversionProgress {
    multi_progress: MultiProgress,
    pub solution_progress: Option<ProgressBar>,
    pub project_progress: Option<ProgressBar>,
    pub file_progress: Option<ProgressBar>,
    current_solution: String,
    current_project: String,
}

impl ConversionProgress {
    /// 创建新的进度显示器
    pub fn new() -> Self {
        Self {
            multi_progress: MultiProgress::new(),
            solution_progress: None,
            project_progress: None,
            file_progress: None,
            current_solution: String::new(),
            current_project: String::new(),
        }
    }
    
    /// 初始化解决方案进度条
    pub fn init_solution_progress(&mut self, solution_count: u64) -> &mut Self {
        let pb = self.multi_progress.add(ProgressBar::new(solution_count));
        pb.set_style(
            ProgressStyle::default_bar()
                .template("{spinner:.green} [{elapsed_precise}] [{bar:40.cyan/blue}] {pos}/{len} 解决方案 ({eta})")
                .unwrap()
                .progress_chars("##-")
        );
        pb.enable_steady_tick(Duration::from_millis(100));
        self.solution_progress = Some(pb);
        self
    }
    
    /// 初始化项目进度条
    pub fn init_project_progress(&mut self, project_count: u64) -> &mut Self {
        let pb = self.multi_progress.add(ProgressBar::new(project_count));
        pb.set_style(
            ProgressStyle::default_bar()
                .template("{spinner:.yellow} [{elapsed_precise}] [{bar:40.yellow/blue}] {pos}/{len} 项目 ({eta})")
                .unwrap()
                .progress_chars("##-")
        );
        pb.enable_steady_tick(Duration::from_millis(100));
        self.project_progress = Some(pb);
        self
    }
    
    /// 初始化文件进度条
    pub fn init_file_progress(&mut self, file_count: u64) -> &mut Self {
        let pb = self.multi_progress.add(ProgressBar::new(file_count));
        pb.set_style(
            ProgressStyle::default_bar()
                .template("{spinner:.green} [{elapsed_precise}] [{bar:40.green/blue}] {pos}/{len} 文件 ({eta})")
                .unwrap()
                .progress_chars("##-")
        );
        pb.enable_steady_tick(Duration::from_millis(100));
        self.file_progress = Some(pb);
        self
    }
    
    /// 更新当前解决方案
    pub fn set_solution(&mut self, solution_name: &str) -> &mut Self {
        self.current_solution = solution_name.to_string();
        if let Some(pb) = &self.solution_progress {
            pb.set_message(format!("当前：{}", solution_name));
        }
        self
    }
    
    /// 更新当前项目
    pub fn set_project(&mut self, project_name: &str) -> &mut Self {
        self.current_project = project_name.to_string();
        if let Some(pb) = &self.project_progress {
            pb.set_message(format!("当前：{}", project_name));
        }
        self
    }
    
    /// 递增解决方案进度
    pub fn inc_solution(&self) {
        if let Some(pb) = &self.solution_progress {
            pb.inc(1);
        }
    }
    
    /// 递增项目进度
    pub fn inc_project(&self) {
        if let Some(pb) = &self.project_progress {
            pb.inc(1);        }
    }
    
    /// 增加文件进度（多个文件）
    pub fn inc_files(&self, count: u64) {
        if let Some(pb) = &self.file_progress {
            pb.inc(count);
        }
    }
    
    /// 完成所有进度
    pub fn finish(&self) {
        if let Some(pb) = &self.solution_progress {
            pb.finish_with_message("所有解决方案转换完成");
        }
        
        if let Some(pb) = &self.project_progress {
            pb.finish_with_message("所有项目转换完成");
        }
        
        if let Some(pb) = &self.file_progress {
            pb.finish_with_message("所有文件处理完成");
        }
    }
    
    /// 解决方案转换失败
    pub fn solution_failed(&self, solution_name: &str, error: &str) {
        if let Some(pb) = &self.solution_progress {
            pb.println(format!("❌ 解决方案 {} 转换失败: {}", solution_name, error));
        }
    }
    
    /// 项目转换失败
    pub fn project_failed(&self, project_name: &str, error: &str) {
        if let Some(pb) = &self.project_progress {
            pb.println(format!("❌ 项目 {} 转换失败: {}", project_name, error));
        }
    }
    
    /// 项目转换成功
    pub fn project_success(&self, project_name: &str) {
        if let Some(pb) = &self.project_progress {
            pb.println(format!("✅ 项目 {} 转换成功", project_name));
        }
    }
} 